package org.project.single.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.project.single.exception.SystemCodeException;
import org.project.single.mapper.SysRoleMapper;
import org.project.single.mapper.SysRoleMenuRelMapper;
import org.project.single.pojo.constant.CacheNameConst;
import org.project.single.pojo.dto.role.CacheRoleDto;
import org.project.single.pojo.po.SysRole;
import org.project.single.pojo.po.SysRoleMenuRel;
import org.project.single.pojo.ro.EnableRo;
import org.project.single.pojo.ro.menu.AuthMenuRo;
import org.project.single.pojo.ro.role.EditRoleBackRo;
import org.project.single.pojo.ro.role.RoleListBackRo;
import org.project.single.pojo.systemEnum.SystemCodeEnum;
import org.project.single.pojo.vo.role.RoleDetailBackVo;
import org.project.single.pojo.vo.role.RoleListBackVo;
import org.project.single.service.SysRoleService;
import org.redisson.api.RBatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stone.project.baseRedis.utils.RedisUtil;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleMenuRelMapper sysRoleMenuRelMapper;

    @Override
    public void editRoleBack(EditRoleBackRo editRoleBackRo) {
        SysRole sysRole = BeanUtil.copyProperties(editRoleBackRo, SysRole.class);
        if (ObjectUtil.isNull(sysRole.getId())){
            sysRole.setId(IdUtil.getSnowflake().nextIdStr());
            sysRole.setCode(IdUtil.getSnowflake().nextIdStr());
            sysRole.setEnableFlag(0);
            sysRole.setDelFlag(0);
            sysRoleMapper.insert(sysRole);
        }else{
            sysRoleMapper.updateById(sysRole);
        }
    }

    @Override
    public Page<RoleListBackVo> getRoleListBack(RoleListBackRo roleListBackRo) {
        Page<RoleListBackVo> res = PageHelper.startPage(roleListBackRo.getPageNum(), roleListBackRo.getPageSize())
                .doSelectPage(() -> {
                    sysRoleMapper.getRoleListBack(roleListBackRo);
                });
        return res;
    }

    @Override
    public RoleDetailBackVo getRoleDatailBack(String id) {
        SysRole sysRole = sysRoleMapper.selectById(id);
        if (ObjectUtil.isNull(sysRole))
            return null;
        RoleDetailBackVo roleDetailBackVo = BeanUtil.copyProperties(sysRole, RoleDetailBackVo.class);
        return roleDetailBackVo;
    }

    @Override
    @Transactional
    public void deleteRole(String id) {
        SysRole sysRole = sysRoleMapper.selectById(id);
        if (ObjectUtil.isNotNull(sysRole)){
            String cacheKey = CacheNameConst.RBAC_ROLE_CACHE_DATA+sysRole.getCode();
            sysRoleMapper.deleteById(id);
        }
        throw new SystemCodeException(SystemCodeEnum.DATA_ERR);
    }

    @Override
    @Transactional
    public void enableRole(EnableRo enableRo) {
        SysRole data = sysRoleMapper.selectById(enableRo.getId());
        if (ObjectUtil.isNotNull(data)){
            SysRole sysRole = BeanUtil.copyProperties(enableRo, SysRole.class);
            sysRoleMapper.updateById(sysRole);
            data.setEnableFlag(sysRole.getEnableFlag());
        }
    }

    @Override
    @Transactional
    public void authMenu(AuthMenuRo authMenuRo) {
        String cacheKey = CacheNameConst.RBAC_ROLE_MENU_CACHE_DATA+authMenuRo.getRoleCode();
        List<SysRoleMenuRel> authMenuList = authMenuRo.getMenuCodeList().stream()
                .map(ele -> {
                    SysRoleMenuRel sysRoleMenuRel = new SysRoleMenuRel();
                    sysRoleMenuRel.setMenuCode(ele);
                    sysRoleMenuRel.setRoleCode(authMenuRo.getRoleCode());
                    return sysRoleMenuRel;
                })
                .collect(Collectors.toList());
        //删除历史授权
        sysRoleMenuRelMapper.delete(Wrappers.lambdaQuery(SysRoleMenuRel.class).eq(SysRoleMenuRel::getRoleCode,authMenuRo.getRoleCode()));
        //添加新授权
        if (CollectionUtil.isNotEmpty(authMenuList)){
            sysRoleMenuRelMapper.insertBatchSomeColumn(authMenuList);
        }
    }
}
